﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Models;
using BLL;
namespace AGVControlSystemPro
{
    public partial class FrmMapOperation : Form
    {
        private FileNameAddressManager objFileNameAddressManager = new FileNameAddressManager();//
        public OpenProject openProjectSender;//根据委托创建委托对象
        public FrmMapOperation()
        {
            InitializeComponent();
            //显示所有存在工程名称
            ProjectNameLoad();
            //按键属性
            if (this.lbProjectName.Items.Count == 0)
            {
                this.btnClear.Enabled = false;
                this.btnDelete.Enabled = false;
                this.btnModify.Enabled = false;
                this.btnOpen.Enabled = false;
            }
        }
        public FrmMapOperation(string id):this()
        {
            this.btnDelete.Enabled = false;
            this.btnModify.Enabled = false;
            this.btnClear.Enabled = false;
        }
        /// <summary>
        /// 文件加载
        /// </summary>
        private void ProjectNameLoad()
        {
            this.lbProjectName.DataSource = objFileNameAddressManager.GetAllFileName();
            this.lbProjectName.DisplayMember = "FileName";
            this.lbProjectName.ValueMember = "ID";
        }
        private void btnOpen_Click(object sender, EventArgs e)
        {
            #region 数据验证
            if (this.lbProjectName.SelectedIndex == -1 || this.lbProjectName.Items.Count ==0)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("プロジェクトファイルを選択してください。", "情報のヒント");
                else MessageBox.Show("请选择工程文件", "信息提示");
                return;
            }
            #endregion
            //数据封装
            FileNameAddress objFileNameAddress = new FileNameAddress() 
            {
                ID = Convert.ToInt32(this.lbProjectName.SelectedValue),
                FileName = this.lbProjectName.Text,
            };
            //4.	通过委托调用办法，而不是直接调用办法
            if (openProjectSender != null)
            {
                openProjectSender(objFileNameAddress);
            }
            this.Close();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            #region 数据验证
            if (this.lbProjectName.SelectedIndex == -1 || this.lbProjectName.Items.Count == 0)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("プロジェクトファイルを選択してください。", "情報のヒント");
                else MessageBox.Show("请选择工程文件", "信息提示");
                return;
            }
            #endregion
            //数据封装
            FileNameAddress objFileNameAddress = new FileNameAddress()
            {
                ID = Convert.ToInt32(this.lbProjectName.SelectedValue),
                FileName = this.lbProjectName.Text
            };
            try
            {
                //打开窗体
                FrmProjectNameEdit objFrmProjectNameEdit = new FrmProjectNameEdit(objFileNameAddress);
                DialogResult result = objFrmProjectNameEdit.ShowDialog();
                if (result == DialogResult.OK)
                {
                    //显示所有存在工程名称
                    ProjectNameLoad();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            #region 数据验证
            if (this.lbProjectName.SelectedIndex == -1 || this.lbProjectName.Items.Count == 0)
            {
                if (CommonMethods.LanguageId == 3) MessageBox.Show("プロジェクトファイルを選択してください。", "情報のヒント");
                else MessageBox.Show("请选择工程文件", "信息提示");
                return;
            }
            #endregion
            //数据封装
            FileNameAddress objFileNameAddress = new FileNameAddress()
            {
                ID = Convert.ToInt32(this.lbProjectName.SelectedValue),
                FileName = this.lbProjectName.Text
            };
            //后台数据访问
            try
            {
                DialogResult result;
                if (CommonMethods.LanguageId == 3) result = MessageBox.Show("削除を確認【" + objFileNameAddress.FileName + "】ファイル?", "情報のヒント", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                else result = MessageBox.Show("确认删除【" + objFileNameAddress.FileName + "】工程吗?", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (result == DialogResult.OK)
                { 
                    //【1】删除地标数据
                    new LandNodeManager().DeleteNodes(objFileNameAddress.ID.ToString());
                    //【2】删除图片
                    new PictureFileManager().DeletePictureFile(objFileNameAddress.ID.ToString());
                    //【3】删除工程名
                    new FileNameAddressManager().DeleteFileName(objFileNameAddress.ID.ToString());
                    //【4】数据重新加载
                    ProjectNameLoad();
                }
            }
            catch (Exception ex)
            {
                
                MessageBox.Show("删除失败！错误详细信息：" + ex.Message);
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            #region 数据验证
            if ( this.lbProjectName.Items.Count == 0)
            {
               // MessageBox.Show("请选择删除的工程文件", "信息提示");
                return;
            }
            #endregion
            //后台数据访问
            try
            {
                DialogResult result;
                if (CommonMethods.LanguageId == 3) result = MessageBox.Show("空のプロジェクトの工事ファイルを確認しますか？", "情報のヒント", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                else result = MessageBox.Show("确认清空项目工程文件吗？", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if ( result== DialogResult.OK)
                {
                    //【1】删除地标数据
                    new LandNodeManager().DeleteNodes("");
                    //【2】删除图片
                    new PictureFileManager().DeletePictureFile("");
                    //【3】删除工程名
                    new FileNameAddressManager().DeleteFileName("");
                    //【4】数据重新加载
                    ProjectNameLoad();
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show("清空工程数据失败！错误详细信息：" + ex.Message);
            }
        }
    }
}
